export function isNull(value: any) {
    return toString.call(value) === '[object Null]'
}

export function randomWord(randomFlag = true, min: number = 32, max: number = 32) {
    let str = "",
        range = min,
        arr = [
            "0",
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "a",
            "b",
            "c",
            "d",
            "e",
            "f"
        ];
    //随机产生
    if (randomFlag) {
        range = Math.round(Math.random() * (max - min)) + min;
    }
    for (let i = 0; i < range; i++) {
        let pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
    }
    return str;
}

export function isEmpty(value: any) {
    if (isUndefined(value) || isNull(value)) {
        return true
    }
    if ((isString(value) || isArray(value)) && value.length === 0) {

        return true
    }
    if (isObject(value) && Object.values(value).length === 0 && !(value instanceof Date)) {
        return true
    }
    if (isObject(value) && value instanceof Date && !value) {
        return true
    }
    return false
}

export function deepClone(target: any) {
    // 定义一个变量
    let result: any
    // 如果当前需要深拷贝的是一个对象的话
    if (typeof target === 'object') {
        // 如果是一个数组的话
        if (Array.isArray(target)) {
            result = [] // 将result赋值为一个数组，并且执行遍历
            for (let i in target) {
                // 递归克隆数组中的每一项
                result.push(deepClone(target[i]))
            }
            // 判断如果当前的值是null的话；直接赋值为null
        } else if (target === null) {
            result = null
            // 判断如果当前的值是一个RegExp对象的话，直接赋值
        } else if (target.constructor === RegExp) {
            result = target
        } else {
            // 否则是普通对象，直接for in循环，递归赋值对象的所有值
            result = {}
            for (let i in target) {
                result[i] = deepClone(target[i])
            }
        }
        // 如果不是对象的话，就是基本数据类型，那么直接赋值
    } else {
        result = target
    }
    // 返回最终结果
    return result
}

export function isObject(value: any) {
    return toString.call(value) === '[object Object]'
}

export function isArray(value: any) {
    return toString.call(value) === '[object Array]'
}

export function isString(value: any) {
    return typeof value === 'string'
}

export function isUndefined(value: any) {
    return typeof value === 'undefined'
}

export function isTrue(value: any) {
    if (isBoolean(value)) {
        return value
    } else if (isNumeric(value)) {
        return value != 0
    } else if (isString(value)) {
        return value === 'true'
    }
    return false
}

export function isBoolean(value: any) {
    return typeof value === 'boolean'
}

export function isFunction(value: any) {
    return typeof value === 'function'
}

export function isNumber(value: any) {
    return !isNaN(value) && typeof value === 'number'
}

export function isNumeric(value: any) {
    return isNumber(Number(value))
}

export function isInteger(value: any, isStrict = true) {
    if (isStrict) {
        return Number.isInteger(value)
    }
    return Number.isInteger(Number(value))
}
